package com.luoc.base;

import com.luoc.base.websocket.DiscardServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Author: luoc
 * @Date: 2019/7/10 9:31
 */
public class DiscardServer {

    private int port;

    public DiscardServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        /**
         *  boss  负责处理客户端进来的连接
         *  worker  handles the traffic of the accepted connection once the boss accepts the connection
         *  and registers the accepted connection to the worker.
         */
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            /**
             * ServerBootstrap is a helper class that sets up a server.
             * You can set up the server using a Channel directly.
             * However, please note that this is a tedious process,
             * and you do not need to do that in most cases.
             */
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    /**
                     * Here, we specify to use the NioServerSocketChannel class
                     * which is used to instantiate a new Channel to accept incoming connections.
                     */
                    .channel(NioServerSocketChannel.class)
                    /**
                     * The handler specified here will always be evaluated by a newly accepted Channel.
                     * The ChannelInitializer is a special handler that is purposed to help a user configure a new Channel.
                     * It is most likely that you want to configure the ChannelPipeline of the new Channel by adding some handlers
                     * such as DiscardServerHandler to implement your network application. As the application gets complicated, it is likely
                     * that you will add more handlers to the pipeline and extract this anonymous class into a top-level class eventually.
                     *
                     */
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new DiscardServerHandler());
                        }
                    }).option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            // Bind and start to accept incoming connections.
            ChannelFuture f = b.bind(port).sync();

            // Wait until the server socket is closed.
            // In this example, this does not happen, but you can do that to gracefully
            // shut down your server.
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port = 8080;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        }

        new DiscardServer(port).run();
    }
}
